﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using COMOSOFT.Class;
using System.Data.SqlClient;
using DevExpress.XtraGrid.Views.Grid;

namespace COMOSOFT.Production.Forms
{
    public partial class FrmDeliveryView : Form
    {
        ClassShare cls = new ClassShare("FrmDeliveryView");       
        int DID,OldValue=-1;
        string info = "";
        public FrmDeliveryView(int _DID)
        {
            InitializeComponent();
            this.DID = _DID;
        }

        private void FrmDeliveryView_Load(object sender, EventArgs e)
        {            
        }

        #region[AddNew]
        public void AddNew()
        {
            deliveryMasterBindingSource.AddNew();
            txt_ShipDate.EditValue = DateTime.Now;
            txt_Handler.EditValue = ClassShare.Usname;
        }
        #endregion

        #region[LoadData]
        public void LoadData()
        {
            sp_CustomerListTableAdapter.Fill(dataSet_Delivery.sp_CustomerList,ClassShare.Usid);
            sp_DeliveryChoosePOTableAdapter.Fill(this.dataSet_Delivery.sp_DeliveryChoosePO);
            factoryListTableAdapter.Fill(this.dataSet_Delivery.FactoryList);
            if (DID > 0)
            {
                deliveryMasterTableAdapter.Fill(this.dataSet_Delivery.DeliveryMaster,DID);
                sp_DeliveryDetailsTableAdapter.Fill(dataSet_Delivery.sp_DeliveryDetails, DID);
                if (groupControl_DeliveryDetial.Enabled == false)
                    groupControl_DeliveryDetial.Enabled = true;
            }
        }
        #endregion

        #region[Update Delivery Master]
        private void dataNavigator_ChoosePO_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            try
            {
                if (e.Button.Tag.ToString() == "Add")
                {
                    SqlConnection con = cls.connection();
                    GridView view = gridView_ChoosePO as GridView;
                    int[] arrOIDs = view.GetSelectedRows();
                    foreach (int index in arrOIDs)
                    {
                        SqlCommand com = new SqlCommand("insert into DeliveryDetail (OID,DID) values (@OID,@DID)", con);
                        com.Parameters.AddWithValue("@OID", Convert.ToInt32(view.GetDataRow(index)["OID"]));
                        com.Parameters.AddWithValue("@DID", DID);
                        com.ExecuteNonQuery();
                    }
                    LoadData();
                }
                else
                {
                    if (e.Button.Tag.ToString() == "Refresh")
                        sp_DeliveryChoosePOTableAdapter.Fill(this.dataSet_Delivery.sp_DeliveryChoosePO);
                    else
                        this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void UpdateData(string com)
        {
            try
            {
                if (com == "Update")
                {
                    deliveryMasterBindingSource.EndEdit();
                    deliveryMasterTableAdapter.Update(dataSet_Delivery.DeliveryMaster);
                    dataSet_Delivery.DeliveryMaster.AcceptChanges();
                    DID = Convert.ToInt32(txt_DID.EditValue);
                    this.Text ="ContNo: "+ txt_ContNo.EditValue.ToString();
                }
                else
                {
                    if (MessageBox.Show("Are you sure delete this object?", "Confirm Delete...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        dataSet_Delivery.DeliveryMaster.Rows[0].Delete();
                        deliveryMasterTableAdapter.Update(dataSet_Delivery.DeliveryMaster);
                        dataSet_Delivery.DeliveryMaster.AcceptChanges();
                    }
                }
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion        

        #region[Update Delivery Details]
        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                GridView view=gridView_DeliveryDetails as GridView;
                if(view.FocusedRowHandle>=0)
                {
                    if (MessageBox.Show("Are you sure delete item?", "Confirm delete...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        SqlConnection con = cls.connection();
                        SqlCommand com = new SqlCommand("delete from DeliveryDetail where DDID=@DDID", con);
                        com.Parameters.AddWithValue("@DDID", Convert.ToInt32(view.GetFocusedDataRow()["DDID"]));
                        com.ExecuteNonQuery();
                        cls.connection().Close();
                    }
                }
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadData();
        }
        #endregion

        private void gridView_DeliveryDetails_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                SqlConnection con = cls.connection();
                if (e.Column.FieldName == "ShipQty")
                {
                    if (!Convert.IsDBNull(e.Value))
                    {
                        if (Convert.ToInt32(e.Value) != OldValue)
                        {
                            info = ClassShare.Usname + " Ship Qty: " + OldValue + " --> " + Convert.ToInt32(e.Value);                            
                            SqlCommand com = new SqlCommand("update DeliveryDetail set ShipQty=@ShipQty, Tracking=@Tracking where DDID=@DDID", con);
                            com.Parameters.AddWithValue("@ShipQty", Convert.ToInt32(e.Value));
                            com.Parameters.AddWithValue("@Tracking", info);
                            com.Parameters.AddWithValue("@DDID", Convert.ToInt32(gridView_DeliveryDetails.GetFocusedDataRow()["DDID"]));
                            com.ExecuteNonQuery();                           
                        }
                    }                   
                }
                else
                {
                    if (e.Column.FieldName == "PackingMethod")
                    {
                        SqlCommand com = new SqlCommand("update DeliveryDetail set PackingMethod=@PM where DDID=@DDID", con);                       
                        com.Parameters.AddWithValue("@PM", e.Value.ToString());
                        com.Parameters.AddWithValue("@DDID", Convert.ToInt32(gridView_DeliveryDetails.GetFocusedDataRow()["DDID"]));
                        com.ExecuteNonQuery();
                    }
                }
                cls.connection().Close();
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gridView_DeliveryDetails_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            if (e.Column.FieldName == "ShipQty")
            {
                if (!Convert.IsDBNull(e.CellValue))
                    OldValue = Convert.ToInt32(e.CellValue);
                else
                    OldValue = 0;
            }
        }
    }
}
